(function($, doc) {
	$.init({});		
	var userinfo = app.getState() || {};
    var uid = userinfo.user_id;
    window.onload = function(){ 
		$.plusReady(function() {
			//关闭等待框
			plus.nativeUI.closeWaiting();
			//显示当前页面
			mui.currentWebview.show();
			
			mui.ajax({
					url: appurl + "Bank/get_bank/",
					data: {'user_id':uid,'etype':2},
					async: true,
					dataType: 'json',
					crossDomain: true, //强制使用5+跨域
					type: 'get',
					timeout: 10000,
					success: function(res) {
						console.log(JSON.stringify(res));
						if(res.is_def==1){
							document.querySelector('.mui-content.zhifubao .mui-switch').className="mui-switch mui-active";
						}
						if(res.img){
							res.img_path=weburl+res.img;
						}
						else{
							res.img_path='';
						}
			
						
						// 请求成功
						doc.querySelector("#weixin-area").innerHTML = template('weixin-template',res);
						
						var setButton = document.getElementById('do_post');
				        var bank_accountBox = document.getElementById('bank_account');
						var bank_cardBox = document.getElementById('bank_card');
						setButton.addEventListener('tap', function() {
							var setInfo = {
								user_id: uid,
								bank_name: '微信',
								bank_account: bank_accountBox.value,
								bank_card: bank_cardBox.value,
								etype: 2
							};
							
							var img_arr=new Array();
							
				            if(document.getElementById("has_upload").value==1){
				            	var base_img=uploadImg(document.getElementById('pinzhen').src);
				            	setInfo.img=base_img;
				            }
				
							if(setInfo.bank_account.trim()=='') {
								plus.nativeUI.toast('请输入姓名');
								return;
							}
				
							if(setInfo.bank_card.trim()=='') {
								plus.nativeUI.toast('请输入账号');
								return;
							}
				            
				            if(document.querySelector('.mui-content.zhifubao .mui-switch').className=='mui-switch mui-active'){
				            	setInfo.is_def=1;
				            }
				            else{
				            	setInfo.is_def=0;
				            }
				         
	
				            //console.log(JSON.stringify(setInfo));
							mui.ajax({
								url: appurl + "Bank/set_weixin/",
								data: setInfo,
								async: true,
								dataType: 'json',
								crossDomain: true, //强制使用5+跨域
								type: 'post',
								timeout: 10000,
								beforeSend: function(data) {
				                    plus.nativeUI.showWaiting(); 
				                },
								success: function(res) {
									plus.nativeUI.closeWaiting(); 
									console.log(JSON.stringify(res));
									// 请求成功
									if(res.status < 1) {
										mui.toast(res.msg);
									} else {
										mui.toast('设置微信成功!');
										mui.fire(plus.webview.getWebviewById('pages/setting/payment_method.html'),'refresh_data');									
										mui.fire(plus.webview.getWebviewById('market.html'), 'refresh_bank');
										mui.back();
									}
								},
								error: function(xhr, type, errorThrown) {
									//console.log(JSON.stringify(xhr));
									//console.log(JSON.stringify(type));
									//console.log(JSON.stringify(errorThrown));
									mui.toast('提交失败！');
								}
							});
							
						}, false);
					},
					error: function(xhr, type, errorThrown) {
						//console.log(JSON.stringify(xhr));
						//console.log(JSON.stringify(type));
						//console.log(JSON.stringify(errorThrown));
						mui.toast('获取数据失败！');
					}
				});
	
					
		});
		
	}
	
	mui('body').on('tap', '.mui-popover-action li>a', function() {
			var a = this,
				parent;
			//根据点击按钮，反推当前是哪个actionsheet
			for (parent = a.parentNode; parent != document.body; parent = parent.parentNode) {
				if (parent.classList.contains('mui-popover-action')) {
					break;
				}
			}
			//关闭actionsheet
			mui('#' + parent.id).popover('toggle');
			if(a.getAttribute('id')=='xiangce'){
				//从相册选择
				galleryImg();
				return false;
			}
			if(a.getAttribute('id')=='paizao'){
				//拍照
				getImage();
				return false;
			}
		})
		
		//拍照
		function getImage() {
			var c = plus.camera.getCamera();
			c.captureImage(function(e) {
				plus.io.resolveLocalFileSystemURL(e, function(entry) {
					var s = entry.toLocalURL() + "?version=" + new Date().getTime();
					console.log(s);
					document.getElementById('pinzhen').style.display='block';
					document.getElementById("pinzhen").src = s;
					document.getElementById("has_upload").value = 1;
					document.getElementById('un_sel_img').style.display="none";
					//uploadImg(s);
					//变更大图预览的src
					//目前仅有一张图片，暂时如此处理，后续需要通过标准组件实现
					document.querySelector("#__mui-imageview__group .mui-slider-item img").src = s + "?version=" + new Date().getTime();
				}, function(e) {
					console.log("读取拍照文件错误：" + e.message);
				});
			}, function(s) {
				console.log("error" + s);
			}, {
				filename: "_doc/head.jpg"
			})
		}
		
		
		//相册
		function galleryImg() {
			plus.gallery.pick(function(a) {
				plus.io.resolveLocalFileSystemURL(a, function(entry) {
					plus.io.resolveLocalFileSystemURL("_doc/", function(root) {
						root.getFile("head.jpg", {}, function(file) {
							//文件已存在
							file.remove(function() {
								console.log("file remove success");
								entry.copyTo(root, 'head.jpg', function(e) {
										var e = e.fullPath + "?version=" + new Date().getTime();
										document.getElementById('pinzhen').style.display='block';
										document.getElementById("pinzhen").src = e;
										document.getElementById("has_upload").value = 1;
										document.getElementById('un_sel_img').style.display="none";
										//uploadImg(e);
										
										//变更大图预览的src
										//目前仅有一张图片，暂时如此处理，后续需要通过标准组件实现
										document.querySelector("#__mui-imageview__group .mui-slider-item img").src = e + "?version=" + new Date().getTime();;
									},
									function(e) {
										console.log('copy image fail:' + e.message);
									});
							}, function() {
								console.log("delete image fail:" + e.message);
							});
						}, function() {
							//文件不存在
							entry.copyTo(root, 'head.jpg', function(e) {
									var path = e.fullPath + "?version=" + new Date().getTime();
									document.getElementById("headimg").src = path;
									//document.getElementById("head-img1").src = path;
									//变更大图预览的src
									//目前仅有一张图片，暂时如此处理，后续需要通过标准组件实现
									document.querySelector("#__mui-imageview__group .mui-slider-item img").src = path;
								},
								function(e) {
									console.log('copy image fail:' + e.message);
								});
						});
					}, function(e) {
						console.log("get _www folder fail");
					})
				}, function(e) {
					console.log("读取拍照文件错误：" + e.message);
				});
			}, function(a) {}, {
				filter: "image"
			})
		}


         //压缩图片   成64位编码
         function uploadImg(path) {
			var img = new Image();
			img.src = path;
			//生成比例 
			var w = img.width,
				h = img.height,
				scale = w / h;
			
			//console.log(img.width);
		    //console.log(img.height);
			w = 480 || w; //480  你想压缩到多大
			h = w / scale;
			
		    //生成canvas
			var canvas = document.createElement('canvas');
			var ctx = canvas.getContext('2d');
			canvas.setAttribute('width', w);
			canvas.setAttribute('height', h);
			ctx.drawImage(img, 0, 0, w, h);
			var base64 = canvas.toDataURL('image/jpeg', 1 || 0.8); //1z 表示图片质量，越低越模糊。	 
			console.log(base64);
			base64 = base64.replace('data:image/jpeg;base64,', '');
			return base64;
		}
         
	

}(mui, document));